package com.ddd.demo.lee.排序;

public class 冒泡排序 {

    public static void main(String[] args) {

        int[] arr = {1, 3, 2, 4, 5};
        System.out.println("排序前的数组为：");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println("");
        bubbleSort(arr);

        System.out.println("排序后的数组为：");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }

    private static void bubbleSort(int[] arr) {
        int n = arr.length;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n -i -1; j++) {
                if(arr[j] < arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }

    }

}
